package com.fenghongzhang.a1811;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



    }

    public void btn(View view) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("标题");
        builder.setMessage("信息");


        //按钮
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });

        AlertDialog alertDialog = builder.create();

        //显示
        alertDialog.show();
    }

    public void btn1(View view) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
//        builder.setMessage("信息");
        //按钮
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //1.7  1.8
        final String[] s = new  String[]{"篮球","足球","乒乓球"};
        //单选
        builder.setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {
            /**
             *
             * @param dialog
             * @param which  数组中的下标
             */
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        //显示
        alertDialog.show();
    }

    public void btn2(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
//        builder.setMessage("信息");
        final String[] s = {"男人","女人","不男不女"};
        final boolean[] b = new boolean[]{true, true, false};
        //按钮
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
                for (int i = 0; i <b.length ; i++) {
                    if(b[i]){
                        Toast.makeText(MainActivity.this, s[i], Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });


        //多选
        builder.setMultiChoiceItems(s, b, new DialogInterface.OnMultiChoiceClickListener() {
            /**
             *
             * @param dialog
             * @param which  下标
             * @param isChecked 是否选中
             */
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {


            }
        });

        AlertDialog alertDialog = builder.create();
        //显示
        alertDialog.show();

    }

    public void btn3(View view) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("标题");
//        builder.setMessage("信息");


        //自定义内容对话框
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null);
        builder.setView(inflate);



        //按钮
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });


        AlertDialog alertDialog = builder.create();

        //显示
        alertDialog.show();

    }

    public void btn4(View view) {
        final ProgressDialog progressDialog = new ProgressDialog(this);

        progressDialog.setMax(100);
        progressDialog.setProgress(10);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int index = 0;
            @Override
            public void run() {
                index += 10;
                progressDialog.setProgress(index);
                if(index >= 100){
                    timer.cancel();
                    progressDialog.dismiss();
                }
            }
        },0,1000);

        progressDialog.show();
    }

    public void btn5(View view) {
        //取年月日
        Calendar calendar = Calendar.getInstance();

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            /**
             *
             * @param view
             * @param year
             * @param month 0 - 11
             * @param dayOfMonth
             */
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this, year+":"+(month+1)+":"+dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }

    public void btn6(View view) {

        Calendar calendar = Calendar.getInstance();

        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
            }
        },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),false).show();
    }

    public void btn7(View view) {
        MyDialog myDialog = new MyDialog(this);
        myDialog.setStrTitle("呵呵呵");
        myDialog.setStrNo("buyao");
        
        myDialog.setYesOnClick(new MyDialog.YesOnClick() {
            @Override
            public void onClick() {
                Toast.makeText(MainActivity.this, "哈哈哈哈", Toast.LENGTH_SHORT).show();
            }
        });
        
        myDialog.setNoOnClick(new MyDialog.NoOnClick() {
            @Override
            public void click() {
                Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_SHORT).show();
            }
        });
        myDialog.show();

    }
}
